package com.suxii.utils;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.os.Build;

import androidx.core.app.NotificationCompat;

import com.suxii.netty_demo.R;
import com.suxii.netty_demo.activity.ChatActivity;


public class NotificationUtils {


    private NotificationManager notificationManager;
    private android.app.Notification notification;

    private static NotificationUtils notificationUtils=null;
    private String channelId;
    private PendingIntent pendingIntent;
    private NotificationCompat.Builder builder;

    // 单例写法
    public static NotificationUtils getInstance(Context context){
        if (notificationUtils==null){
            notificationUtils=new NotificationUtils();
            notificationUtils.addNotice(context);
        }
        return notificationUtils;
    }


    private void addNotice(Context context) {
        //this.context=context;

        // 创建PendingIntent
        Intent intent = new Intent(context, ChatActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);

        // 创建通知渠道
        channelId = "notice_channel_id";
        String channelName = "即时通讯";
        notificationManager = context.getSystemService(NotificationManager.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }

        // 构建通知内容
        builder = new NotificationCompat.Builder(context, channelId);

        builder.setSmallIcon(R.drawable.ic_launcher_background)
                .setContentIntent(pendingIntent)
                //.setLargeIcon(BitmapFactory.decodeResource(Resources.getSystem(),R.drawable.default_tx))
                .setAutoCancel(true);
    }

    public void sendNotice(int uid,String title,String message){

        Global.noticeUid = uid;
        // 构建通知内容
        builder.setContentTitle(title)
                .setContentText(message);
        notification = builder.build();

        int notificationId = 1;
        notificationManager.notify(notificationId, notification);
    }
}
